/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.guilayer.gamemanagermenu;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import it.unical.canonaco.rende.igpe.game.cbv.guilayer.components.AbstractPainterManager;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.components.GUIGenericPawn;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.components.ImageProvider;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.components.OverallGUILayoutFactory;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.guievents.CharacterClickEvent;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.guievents.ClickedMapCellEvent;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.guievents.GraphicEventManager;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.Character;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.world.Chessboard;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.world.Map;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.exceptions.InvalidFileException;

import javax.swing.JPanel;

/**
 * @author Massimo Canonaco
 *
 */
public class MapAreaPanel extends JPanel 
{
	/** */
	private boolean click; 
	
	private AbstractPainterManager painter;
	
	private Point clickedPoint;
	
	private float clickedOpacity;
	private float clickedOpacityMIN;
	private float clickedOpacityMAX;
	private Color clickedPointColor; 
	
	private float add; 

	protected Chessboard world;
	
	protected final int PIXEL_TILE;
	
	public MapAreaPanel(final  Map map, final java.util.Map<String,String> settings, final List<GUIGenericPawn> pawnsList, AbstractPainterManager cpi)
	{
		super();
		click = false;
		painter = cpi;
		final String PIXEL_TILE_STRING = "maparea.tile",
				PAINTER_CLICK_COLOR = "painter.info.clickedpoint.color",
				PAINTER_CLICK_OPACITY_MIN = "painter.info.clickedpoint.opacity.min",
				PAINTER_CLICK_OPACITY_MAX = "painter.info.clickedpoint.opacity.max",
				PAINTER_ANIMATION_ADD = "painter.info.animation.add.value";
		world = map.getArena();
		
		clickedOpacityMIN = Float.parseFloat(settings.get(PAINTER_CLICK_OPACITY_MIN));
		clickedOpacity = clickedOpacityMIN;
		clickedOpacityMAX = Float.parseFloat(settings.get(PAINTER_CLICK_OPACITY_MAX));
		clickedPointColor = OverallGUILayoutFactory.getInstance().getParsedColor(settings.get(PAINTER_CLICK_COLOR));
		add = Float.parseFloat(settings.get(PAINTER_ANIMATION_ADD));
		PIXEL_TILE = Integer.parseInt((String)settings.get(PIXEL_TILE_STRING));
		clickedPoint = new Point(-1,-1);
//		this.addMouseListener(mouseListener);
		this.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent arg0) {}
			
			@Override
			public void mousePressed(MouseEvent arg0) 
			{
				painter.coordinates(arg0.getX(), arg0.getY(), PIXEL_TILE, clickedPoint);
				

				if(world.getPawn(clickedPoint.x, clickedPoint.y) !=  null)
				{
					CharacterClickEvent event = new CharacterClickEvent((Character)world.getPawn(clickedPoint.x, clickedPoint.y));
//					System.out.println("CLICK ON PAWN");
					GraphicEventManager.getInstance().fireEvent(event);
				}
				
				ClickedMapCellEvent cellEvent = new ClickedMapCellEvent();
				cellEvent.setRow(clickedPoint.x);
				cellEvent.setColumn(clickedPoint.y);
				GraphicEventManager.getInstance().fireEvent(cellEvent);
				click = true;
			}
			
			@Override
			public void mouseExited(MouseEvent arg0) {}
			
			@Override
			public void mouseEntered(MouseEvent arg0) {}
			
			@Override
			public void mouseClicked(MouseEvent arg0) {}
		});
	}
	@Override
	protected void paintComponent(Graphics g) 
	{
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		g2.setColor(clickedPointColor);
		try 
		{
			g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
			painter.paintScene(PIXEL_TILE, world, g2);
			if(click)
			{
				g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, clickedOpacity));
				painter.paintClickPoint(clickedPoint.x, clickedPoint.y, PIXEL_TILE, clickedOpacity, g2);
			}
		} catch (InvalidFileException e) 
		{
			e.printStackTrace();
		}
		
//		System.out.println("painting");
	}
	
	/** */
	public void updateMapGUI()
	{

		painter.updateStatus();
		float result = clickedOpacity + add;
		
		if(result >= clickedOpacityMAX)
		{
			add = - add;
		}
		else if(result <= clickedOpacityMIN)
		{
			add = Math.abs(add);
		}
		
		clickedOpacity += add;
	}

	/**
	 * @return the pIXEL_TILE
	 */
	public int getPIXEL_TILE() 
	{
		return PIXEL_TILE;
	}

	/** */
	public AbstractPainterManager getPainter()
	{
		return painter;
	}
	
//	public void setting(Map map)
//	{
//		world = map.getArena();
//	}
}
